<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/8/28
 * Time: 16:52
 */
namespace app\admin\controller;
use think\Db;
use app\admin\model\BusinessModel;
class Business extends Base
{
    /*
     * 商铺数据
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($type)&&$type != ""){
                $map['s_status'] = $type;
            }
            if(isset($key)&&$key!="")
            {
                $map['s_name|s_phone|s_address'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $user = new BusinessModel();
            $count = $user->getAllCount($map);
            $lists = $user->getBusinessData($map,$Nowpage, $limits);
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        if(session('?s_id')){
            session('s_id',null);
        }
        return $this->fetch('business/index');
    }

    /*
     * 活动数据
     */
    public function active(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            $map['sa_shopid'] = session('s_id');
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['sa_start'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['sa_start'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['sa_start'] = ['between time',[$start,$end]];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $user = new BusinessModel();
            $count = $user->getActiveCount($map);
            $lists = $user->getActiveData($map,$Nowpage, $limits);
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        if(!session('?s_id')){
            session('s_id',input('id'));
        }
        return $this->fetch('business/active');
    }

    /*
     * 广告数据
     */
    public function advertise(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['adv_title|adv_desc'] = ['like',"%" . $key . "%"];
            }
            $map['adv_shopid'] = session('s_id');
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $user = new BusinessModel();
            $count = $user->getAdvertiseCount($map);
            $lists = $user->getAdvertiseData($map,$Nowpage, $limits);
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        if(!session('?s_id')){
            session('s_id',input('id'));
        }
        return $this->fetch('business/advertise');
    }

    /*
     * 店铺分类数据
     */
    public function shopType(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['st_name'] = ['like',"%" . $key . "%"];
            }
            $map['st_shopid'] = session('s_id');
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $user = new BusinessModel();
            $count = $user->getTypeCount($map);
            $lists = $user->getTypeData($map,$Nowpage, $limits);
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        if(!session('?s_id')){
            session('s_id',input('id'));
        }
        return $this->fetch('business/shop_type');
    }

    /*
     * 账户信息
     */
    public function account(){
        $data = Db::name('shop_account')
            ->where('acc_shopid',input('id'))
            ->field('acc_id,acc_username,acc_idcard,acc_bank,acc_number')
            ->select();
        return $this->fetch('business/account',['data'=>$data]);
    }

    /*
     * 商户信息
     */
    public function shopInfo(){
        $data = Db::name('shops')
            ->where('s_id',input('id'))
            ->find();
        return $this->fetch('business/shop_info',['data'=>$data]);
    }

    /*
     * 骑手数据
     */
    public function driver(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['d_name|d_phone'] = ['like',"%" . $key . "%"];
            }
            $map['d_shop_id'] = session('s_id');
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('shop_driver')
                ->where($map)
                ->count();
            $lists = Db::name('shop_driver')
                ->field('d_id,d_name,d_phone,d_money,d_time')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('d_time desc')
                ->select();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        if(!session('?s_id')){
            session('s_id',input('id'));
        }
        return $this->fetch ('business/driver');
    }

    /*
     * 分类图标首页
     */
    public function font(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['create_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['create_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['create_time'] = ['between time',[$start,$end]];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('admin_tubiao')
                ->where($map)
                ->count();
            $lists = Db::name('admin_tubiao')
                ->field('id,thumb,create_time,update_time')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('create_time desc')
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('business/font');
    }

    /*
     * 添加分类图标
     */
    public function fontAdd(){
        if(request()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param = [
                'thumb'=>$layimg,
                'create_time'=>$time,
                'update_time'=>$time
            ];
            $res = Db::name('admin_tubiao')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return $this->fetch('business/font_add');
    }

    /*
     * 修改分类图标
     */
    public function fontEdit(){
        if(request()->isPost ()){
            extract(input());
            $param = [
                'thumb'=>$layimg,
                'update_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('admin_tubiao')->where('id',$id)->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        $data = Db::name('admin_tubiao')->where('id',input('id'))->find();
        return $this->fetch('business/font_edit',['data'=>$data]);
    }

    /*
     * 删除图标
     */
    public function fontDel(){
        $res = Db::name('admin_tubiao')->where('id',input('id'))->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    /*
     * 行业分类首页数据
     */
    public function industry(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['pl_name'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('work_pl')
                ->where($map)
                ->count();
            $lists = Db::name('work_pl')
                ->field('pl_id,pl_name,pl_create_time,pl_update_time,pl_image,pl_order')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('pl_order desc')
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('business/industry');
    }

    /*
     * 添加行业
     */
    public function industryAdd(){
        if(request()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param = [
                'pl_name'=>$name,
                'pl_image'=>$pl_image,
                'pl_order'=>$pl_order,
                'pl_create_time'=>$time,
                'pl_update_time'=>$time
            ];
            $res = Db::name('work_pl')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return $this->fetch('business/industry_add');
    }

    /*
     * 修改行业
     */
    public function industryEdit(){
        if(request()->isPost ()){
            extract(input());
            $param = [
                'pl_name'=>$name,
                'pl_image'=>$pl_image,
                'pl_order'=>$pl_order,
                'pl_update_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('work_pl')->where('pl_id',input('id'))->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        $data = Db::name('work_pl')->where('pl_id',input('id'))->find();
        return $this->fetch('business/industry_edit',['data'=>$data]);
    }

    /*
     * 删除行业
     */
    public function industryDel(){
        $res = Db::name('work_pl')->where('pl_id',input('id'))->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    /*
     * 商户关停和启用
     */
    public function businessState(){
        extract(input());
        if($num == 2){
            $msg = '关停';
        }else{
            $msg = '启用';
        }
        $res = Db::name('shops')->where('s_id',$id)->setField ('s_admin',$num);
        if($res){
            return json(['code'=>200,'msg'=>$msg.'成功']);
        }else{
            return json(['code'=>100,'msg'=>$msg.'失败']);
        }
    }
}